/**
 * 通知公告
 */
import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { SysNotice, SysNoticeParam } from './model';

/**
 * 列表
 */
export async function page(params?: SysNoticeParam) {
  const res = await request.post<ApiResult<PageResult<SysNotice>>>(
    '/sysNotice/page',
    { params }
  );
  if (res.data.code === 200) {
    return res.data.result;
  }
  return Promise.reject(new Error(res.data.message));
}
/**
 * 发布
 */
export async function publicPost(id: number | string) {
  const res = await request.post<ApiResult<any>>('/sysNotice/public', { id });
  if (res.data.code === 200) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 新增
 */
export async function add(data: SysNotice) {
  const res = await request.post<ApiResult<any>>('/sysNotice/add', data);
  if (res.data.code === 200) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 更新
 */
export async function update(data: SysNotice) {
  const res = await request.post<ApiResult<any>>('/sysNotice/update', data);
  if (res.data.code === 200) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除
 */
export async function remove(id: number | string) {
  const res = await request.post<ApiResult<any>>('/sysNotice/delete', { id });
  if (res.data.code === 200) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 未读列表
 */
export async function unReadList(params?: SysNoticeParam) {
  const res = await request.get<ApiResult<SysNotice[]>>(
    '/sysNotice/unReadList',
    {
      params
    }
  );
  if (res.data.code === 200) {
    return res.data.result;
  }
  return Promise.reject(new Error(res.data.message));
}
